<?php

require_once('servicio_web.php');
App::uses('AppController', 'Controller');


class WebServiceController extends AppController {
	var $name = 'WebService';
	var $uses=array('Film');
	
	
	
/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$f=webfilm($id);
		if ($f==null) {
			throw new NotFoundException(__('Invalid film'));
		}
		$this->set('id',$f->id);
		$this->set('title',$f->title);
		$this->set('synopsis',$f->synopsis);
		$this->set('imagen',$f->posters->detailed);
		$this->set('release_dates',$f->release_dates->theater);
		$this->set('duration',$f->runtime);
		$this->set('calification',(int)($f->ratings->critics_score/10));
		$this->set('Genre',$f->genres);

		$people=array();
		$i=0;
		foreach ($f->abridged_cast as $actor){
			if($aux=$this->Film->Actor->find('first',array('fields' => array('id','name'),'recursive'=>'-1','conditions' => array('name' => $actor->name)))){
				$people[$i]=array('exist'=>1,'name'=>$aux['Actor']['name'],'id'=>$aux['Actor']['id']);
				$i++;
			}else{
				$people[$i]=array('exist'=>0,'name'=>$actor->name,'id'=>$actor->id);
				$i++;
			}
		}
		$this->set('actors',$people);
		
		$people=array();
		$i=0;
		foreach ($f->abridged_directors as $director){
			if($aux=$this->Film->Director->find('first',array('fields' => array('id','name'),'recursive'=>'-1','conditions' => array('name' => $director->name)))){
				$people[$i]=array('exist'=>1,'name'=>$aux['Director']['name'],'id'=>$aux['Director']['id']);
				$i++;
			}else{
				$people[$i]=array('exist'=>0,'name'=>$director->name);
				$i++;
			}
		}
		$this->set('directors',$people);
		
		$f=webfilmrelated($id);
		if($f==null){
			return;
		}
		$related=array();
		$i=0;
		//var_dump($f);
		foreach ($f as $movie){
			if($aux=$this->Film->find('first',array('fields' => array('id','title'),'recursive'=>'-1','conditions' => array('title' => $movie->title)))){
				$related[$i]=array('exist'=>1,'title'=>$aux['Film']['title'],'id'=>$aux['Film']['id']);
			}else
				$related[$i]=array('title'=>$movie->title,'id'=>$movie->id,'exist'=>'0');
			$i++;
		}
		$this->set('related',$related);
	}
	
	
}




?>